package com.feicheng.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.base.common.DaoConstant;
import com.feicheng.base.result.Result;
import com.feicheng.entity.User;
import com.feicheng.entity.Warehouse;
import com.feicheng.model.UserWithRoleWithPermission;
import com.feicheng.service.impl.UserServiceImpl;
import com.feicheng.service.impl.WarehouseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yvming.gao
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/user")
public class UserController {

    /*注入用户Service*/
    @Autowired
    UserServiceImpl userService;

    /*注入仓库Service*/
    @Autowired
    WarehouseServiceImpl warehouseService;

    /**
     * 登录验证
     * @param user 用户信息
     * @return
     */
    @PostMapping("/info/g")
    public Result getUserInfo(@RequestBody User user) {

        /*验证账号，密码是否为空*/
        if (0 == user.getAccount().length() || 0 == user.getPassword().length()) {
            return Result.success(false);
        }

        //构建动态检索条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        QueryWrapper<UserWithRoleWithPermission> queryWrapper1 = new QueryWrapper<>();
        List<Object> list = new ArrayList<>();

        //将账号，密码作为查询条件
        queryWrapper.eq(DaoConstant.USER.ACCOUNT,user.getAccount()).eq(DaoConstant.USER.PASSWORD,user.getPassword());
        Map<String,Object> map = userService.getMap(queryWrapper);

        /*判断是否查询到数据*/
        if (null == userService.getMap(queryWrapper)){
            return Result.success(false);
        }

        /*根据用户编号查询仓库信息*/
        Integer u = (Integer) map.get(DaoConstant.STORE_USER.USER_ID);
        warehouseService.getWarehouseInfo(u);

        /*根据用户编号查询角色，权限信息*/
        queryWrapper1.eq(DaoConstant.STORE_USER.USER_ID,u);
        userService.getUserInfo(queryWrapper1);

        /*验证成功返回数据*/
        list.add(userService.getMap(queryWrapper));
        list.add(warehouseService.getWarehouseInfo(u));
        list.add(userService.getUserInfo(queryWrapper1));
        return Result.success(list);
    }
}